package com.huey.utils.mybatisPlus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.huey.utils.mybatisPlus.enums.SexEnum;
import com.huey.utils.mybatisPlus.pojo.UserDO;
import com.huey.utils.mybatisPlus.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xubo
 * @date 2022/8/9 16:47
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * description: 新增

     * @return: boolean
     */
    @RequestMapping("/save")
    public void save() {
        for(int i=1000000;i<=2000000;i++){
            UserDO userDO = new UserDO();
            userDO.setNickname(String.valueOf(i));
            userDO.setSex(SexEnum.MAN);
            userDO.setVersion(8);
            userDO.setIsDelete(1);
            userService.save(userDO);
        }
    }

    /**
     * description: 修改
     * @param nickname
     * @param id
     * @return: boolean
     */
    @RequestMapping("/update")
    public boolean update(@RequestParam String nickname,@RequestParam Long id) {
        UserDO userDO = new UserDO();
        userDO.setNickname(nickname);
        userDO.setId(id);
        userService.saveOrUpdateBatch(Lists.newArrayList(userDO));
        return userService.updateById(userDO);
    }

    /**
     * description: 删除
     * @param id
     * @return: boolean
     */
    @RequestMapping("/delete")
    public boolean delete(@RequestParam Long id) {
        UserDO userDO = new UserDO();
        userDO.setId(id);
        return userService.removeById(userDO);
    }

    /**
     * description: 列表
     * @return: java.util.List<com.wjbgn.user.entity.UserDO>
     */
    @RequestMapping("/list")
    public List<UserDO> list() {
        return userService.list();
    }

    /**
     * description: 分页列表
     * @param current
     * @param size
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page
     */
    @RequestMapping("/page")
    public Page page(@RequestParam int current, @RequestParam int size) {
        return userService.page(new Page<>(current,size), new QueryWrapper(new UserDO()));
    }
}
